package com.lam.code.configuration.shiro;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.lam.code.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;

/**
 * @author lam
 * @description JWTCredentialsMatcher
 * @date 2019/12/25 0:34
 */
@Slf4j
public class JWTCredentialsMatcher implements CredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        String token = (String) authenticationToken.getCredentials();
        String salt = authenticationInfo.getCredentials().toString();
        User user = (User)authenticationInfo.getPrincipals().getPrimaryPrincipal();
        try {
           return JWTUtil.verify(token, user.getUserCode(), salt);
        } catch (JWTVerificationException e) {
            log.error("Token Error:{}", e.getMessage());
        }
        return false;
    }

}
